#!/bin/bash

echo "==== 测试Foldseek结构搜索API ===="
echo ""

# 1. 测试从数据库选择(使用已知accession)
echo "测试1: 从数据库选择蛋白质结构搜索"
echo "----------------------------------------"
curl -X POST "http://localhost:8000/api/search/structure/" \
  -F "query_accession=WP_042059674.1" \
  -F "tm_score_threshold=0.7" \
  -F "max_results=10" \
  2>/dev/null | python3 -m json.tool | head -50

echo ""
echo ""

# 2. 测试缓存
echo "测试2: 重复查询测试缓存"
echo "----------------------------------------"
curl -X POST "http://localhost:8000/api/search/structure/" \
  -F "query_accession=WP_042059674.1" \
  -F "tm_score_threshold=0.7" \
  -F "max_results=10" \
  2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); et=d.get('execution_time'); print(f\"Cached: {d.get('cached')}, Time: {et if et is None else f'{et:.3f}'}s, Hits: {d.get('total_hits')}\")"

echo ""
echo ""

# 3. 测试上传PDB文件
echo "测试3: 上传PDB文件搜索"
echo "----------------------------------------"
# 使用数据库中第一个PDB文件作为测试
TEST_PDB=$(ls /root/dev_folder/boid_database/foldseek_db/pdb_files/*.pdb | head -1)
if [ -f "$TEST_PDB" ]; then
    echo "使用测试文件: $(basename $TEST_PDB)"
    curl -X POST "http://localhost:8000/api/search/structure/" \
      -F "pdb_file=@$TEST_PDB" \
      -F "tm_score_threshold=0.8" \
      -F "max_results=10" \
      2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); et=d.get('execution_time'); print(f\"Success: {d.get('success')}, Time: {et if et is None else f'{et:.3f}'}s, Hits: {d.get('total_hits')}\")"
else
    echo "未找到测试PDB文件"
fi

echo ""
echo ""

# 4. 测试输入验证 - 缺少参数
echo "测试4: 输入验证 - 缺少参数"
echo "----------------------------------------"
curl -X POST "http://localhost:8000/api/search/structure/" \
  -F "tm_score_threshold=0.5" \
  2>/dev/null | python3 -m json.tool

echo ""
echo ""

# 5. 测试不存在的accession
echo "测试5: 不存在的accession"
echo "----------------------------------------"
curl -X POST "http://localhost:8000/api/search/structure/" \
  -F "query_accession=INVALID_ACC" \
  -F "tm_score_threshold=0.5" \
  2>/dev/null | python3 -m json.tool

echo ""
echo "==== 测试完成 ===="

